﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileDFS.Model;
using System.Threading;
using System.IO;
/*
 * Coder:Tieria
 * WriteTime:2015.5.14
 * FileName:DeleteHelper.cs
 * ReMark:删除文件或图片的操作
 */
namespace FileDFS.Utils
{
    public class DeleteHelper
    {
        public static Queue<DeleteQueueModel> DelQue;
        static DeleteHelper()
        {
            DelQue = new Queue<DeleteQueueModel>();
            ThreadPool.QueueUserWorkItem(u =>
            {
                while (true)
                {
                    DeleteQueueModel delModel = null;
                    if (DelQue == null)
                    {
                        continue;
                    }
                    lock (DelQue)
                    {
                        if (DelQue.Count > 0)
                        {
                            delModel = DelQue.Dequeue();
                        }
                    }
                    //将文件进行删除的操作
                    if (delModel != null && !string.IsNullOrEmpty(delModel.AbsoluteFullPath))
                    {
                        if (File.Exists(delModel.AbsoluteFullPath))
                        {
                            File.Delete(delModel.AbsoluteFullPath);
                        }
                    }
                    if (DelQue.Count() <= 0)
                    {
                        Thread.Sleep(50);
                    }
                }
            });
        }
        /// <summary>
        /// 将删除文件添加到队列
        /// </summary>
        /// <param name="delQueue"></param>
        public static void AddDelete(DeleteQueueModel delQueue)
        {
            lock (DelQue)
            {
                DelQue.Enqueue(delQueue);
            }
        }
    }
}
